home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’96 / Talking Telnet / source / Speech / SpeakSelectedText.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-22  |  1.6 KB  |  85 lines  |  [TEXT/CWIE]

  1. #include    "speech.proto.h"
  2. #include    "rsinterf.proto.h"
  3. #include    "rsdefs.h"
  4. #include    <Errors.h>
  5. #include    <Gestalt.h>
  6. #include    <Memory.h>
  7. #include    <Menus.h>
  8.  
  9.  
  10. Boolean SpeakSelectedText(short w)
  11. {
  12.     Boolean    isSpeaking = false;
  13.     
  14.     do {
  15.         
  16.         OSErr    err;
  17.         long    response;
  18.         Handle    newSpeechText;
  19.         
  20.         /* Is the Speech Manager present? */
  21.         
  22.         err = Gestalt(gestaltSpeechAttr, &response);
  23.         if (noErr != err || !((1L << gestaltSpeechMgrPresent) & response)) {
  24.             break;
  25.         }
  26.         
  27.         /* Is there any text selected? */
  28.         
  29.         newSpeechText = RSGetTextSel(w, 0);
  30.         if (nil == newSpeechText) {
  31.             break;
  32.         }
  33.         
  34.         
  35.         do {
  36.             Size    numberSpeechTextBytes;
  37.             
  38.             /* Get a speech channel */
  39.             
  40.             if (nil == gSpeechChannel) {
  41.                 MoveHHi((Handle)gVoices);
  42.                 HLock((Handle)gVoices);
  43.                 err = NewSpeechChannel(&(*gVoices)[gSelectedVoiceIndex], &gSpeechChannel);
  44.                 HUnlock((Handle)gVoices);
  45.                 if (noErr != err) {
  46.                     break;
  47.                 }
  48.             }
  49.             
  50.             /* Start speaking the new text */
  51.             
  52.             MoveHHi(newSpeechText);
  53.             HLock(newSpeechText);
  54.             numberSpeechTextBytes = GetHandleSize(newSpeechText);
  55.             
  56.             err = SpeakText(gSpeechChannel, *newSpeechText, numberSpeechTextBytes);
  57.             
  58.             /* Flash the selection, and fix the menu */
  59.             
  60.             FlashSelection(w);
  61.             EnableItem(myMenus[Speech], SPStopSpeaking);
  62.             
  63.             /* 
  64.             
  65.             /* Free the old buffer, and set the global to the new one */
  66.             
  67.             DisposeHandle(gSpeechText);
  68.             gSpeechText = newSpeechText;
  69.  
  70.             isSpeaking = true;
  71.             
  72.         } while (false);
  73.         
  74.         if (!isSpeaking) {
  75.             if (nil != newSpeechText) {
  76.                 DisposeHandle(newSpeechText);
  77.             }
  78.         }
  79.  
  80.     } while (false);
  81.     
  82.     return isSpeaking;
  83. }
  84.  
  85.